A property, in some object-oriented programming languages, is a special sort of class member, intermediate between a field (or data member) and a method. Properties are read and written like fields, but property reads and writes are (usually) translated to get and set method calls. The field-like syntax is said to be easier to read and write than lots of method calls, yet the interposition of method calls allows for data validation, active updating (as of GUI visuals), or read-only 'fields'. That is, properties are intermediate between member code (methods) and member data (instance variables) of the class, and properties provide a higher level of encapsulation than public fields.
Contents |
Programming languages that support properties include ActionScript 3, C#, D, Delphi/Free Pascal, eC, F#, JavaScript, Objective-C 2.0, Python, Scala, Vala, and Visual Basic. Some object-oriented languages, such as Java, don't support properties, and require the programmer to define a pair of accessor and mutator methods instead. Oberon-2 provides an alternative mechanism using object variable visibility flags. Other languages designed for the Java Virtual Machine, such as Groovy, do natively support properties. While C++ doesn't have first class properties, they can be emulated due to operator overloading. Also note that some C++ compilers support first class properties (the Microsoft C++ compiler as an example).
In most languages, properties are implemented as a pair of accessor/mutator methods, but accessed using the same syntax as for public fields. Omitting a method from the pair yields a read-only or an uncommon write-only property.
In some languages with no built-in support for properties, a similar construct can be implemented as a single method that either returns or changes the underlying data, depending on the context of its invocation. Such techniques are used e.g. in Perl.
Some languages (Ruby, Smalltalk) achieve property-like syntax using normal methods, sometimes with a limited amount of syntactic sugar.
class Pen { private int m_Color; // private field public int Color { // public property get { return m_Color; } set { m_Color = value; } } }
// accessing: Pen pen = new Pen(); // ... pen.Color = ~pen.Color; // bitwise complement ... // another silly example: pen.Color += 1; // a lot clearer than "pen.set_Color(pen.get_Color() + 1)"!
Recent C# versions also allow "auto-implemented properties" where the backing field for the property is generated by the compiler during compilation. This means that the property must have a setter, however it can be private.
class Shape { public Int32 Height { get; set; } public Int32 Width { get; private set; } }
C++ does not have first class properties, but there exist several ways to emulate properties to a limited degree. Two of which follow:
#include <iostream> template <typename T> class property { T value; public: T & operator = (const T &i) { ::std::cout << i << ::std::endl; return value = i; } // This template class member function template serves the purpose to make // typing more strict. Assignment to this is only possible with exact identical // types. template <typename T2> T2 & operator = (const T2 &i) { ::std::cout << "T2: " << i << ::std::endl; T2 &guard = value; throw guard; // Never reached. } operator T const & () const { return value; } }; struct Foo { // Properties using unnamed classes. class { int value; public: int & operator = (const int &i) { return value = i; } operator int () const { return value; } } alpha; class { float value; public: float & operator = (const float &f) { return value = f; } operator float () const { return value; } } bravo; }; struct Bar { // Using the property<>-template. property <bool> alpha; property <unsigned int> bravo; }; int main () { Foo foo; foo.alpha = 5; foo.bravo = 5.132f; Bar bar; bar.alpha = true; bar.bravo = true; // This line will yield a compile time error // due to the guard template member function. ::std::cout << foo.alpha << ", " << foo.bravo << ", " << bar.alpha << ", " << bar.bravo << ::std::endl; return 0; }
An example taken from the MSDN documentation page.
// declspec_property.cpp struct S { int i; void putprop(int j) { i = j; } int getprop() { return i; } __declspec(property(get = getprop, put = putprop)) int the_prop; }; int main() { S s; s.the_prop = 5; return s.the_prop; }
class Pen { private int m_color; // private field // public get property public int color () { return m_color; } // public set property public int color (int value) { return m_color = value; } }
auto pen = new Pen; pen.color = ~pen.color; // bitwise complement // the set property can also be used in expressions, just like regular assignment int theColor = (pen.color = 0xFF0000);
In D version 2, each property accessor or mutator must be marked with @property:
class Pen { private int m_color; // private field // public get property @property public int color () { return m_color; } // public set property @property public int color (int value) { return m_color = value; } }
type TPen = class private m_Color: Integer; function Get_Color: Integer; procedure Set_Color(RHS: Integer); public property Color: Integer read Get_Color write Set_Color; end; function TPen.Get_Color: Integer; begin Result := m_Color end; procedure TPen.Set_Color(RHS: Integer); begin m_Color := RHS end;
// accessing: var pen: TPen; // ... pen.Color := not pen.Color; (* Delphi also supports a 'direct field' syntax - property Color: Integer read m_Color write Set_Color; or property Color: Integer read Get_Color write m_Color; where the compiler generates the exact same code as for reading and writing a field. This offers the efficiency of a field, with the safety of a property. (You can't get a pointer to the property, and you can always replace the member access with a method call.) *)
GeSHi Error: GeSHi could not find the language fsharp (using path /usr/share/php-geshi/geshi/) (code 2)
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
abap, actionscript, actionscript3, ada, apache, applescript, apt_sources, asm, asp, autoit, avisynth, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_mac, caddcl, cadlisp, cfdg, cfm, cil, cmake, cobol, cpp, cpp-qt, csharp, css, d, dcs, delphi, diff, div, dos, dot, eiffel, email, erlang, fo, fortran, freebasic, genero, gettext, glsl, gml, gnuplot, groovy, haskell, hq9plus, html4strict, idl, ini, inno, intercal, io, java, java5, javascript, kixtart, klonec, klonecpp, latex, lisp, locobasic, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, make, matlab, mirc, modula3, mpasm, mxml, mysql, nsis, oberon2, objc, ocaml, ocaml-brief, oobas, oracle11, oracle8, pascal, per, perl, php, php-brief, pic16, pixelbender, plsql, povray, powershell, progress, prolog, properties, providex, python, qbasic, rails, rebol, reg, robots, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, tcl, teraterm, text, thinbasic, tsql, typoscript, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xml, xorg_conf, xpp, z80
GeSHi Error: GeSHi could not find the language fsharp (using path /usr/share/php-geshi/geshi/) (code 2)
You need to specify a language like this: <source lang="html4strict">...</source>
Supported languages for syntax highlighting:
abap, actionscript, actionscript3, ada, apache, applescript, apt_sources, asm, asp, autoit, avisynth, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_mac, caddcl, cadlisp, cfdg, cfm, cil, cmake, cobol, cpp, cpp-qt, csharp, css, d, dcs, delphi, diff, div, dos, dot, eiffel, email, erlang, fo, fortran, freebasic, genero, gettext, glsl, gml, gnuplot, groovy, haskell, hq9plus, html4strict, idl, ini, inno, intercal, io, java, java5, javascript, kixtart, klonec, klonecpp, latex, lisp, locobasic, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, make, matlab, mirc, modula3, mpasm, mxml, mysql, nsis, oberon2, objc, ocaml, ocaml-brief, oobas, oracle11, oracle8, pascal, per, perl, php, php-brief, pic16, pixelbender, plsql, povray, powershell, progress, prolog, properties, providex, python, qbasic, rails, rebol, reg, robots, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, tcl, teraterm, text, thinbasic, tsql, typoscript, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xml, xorg_conf, xpp, z80
function Pen() { this._color = 0; } // Add the property to the Pen type itself, can also // be set on the instance individually Object.defineProperties(Pen.prototype, { color: { get: function () { return this._color; }, set: function (value) { this._color = value; } } });
var pen = new Pen(); pen.color = ~pen.color; // bitwise complement pen.color += 1; // Add one
@interface Pen : NSObject { NSColor *color; } @property(copy) NSColor *color; // color values always copied. @end @implementation Pen @synthesize color; // synthesize accessor methods. @end // Example Usage Pen *pen = [[Pen alloc] init]; pen.color = [NSColor blackColor]; float red = pen.color.redComponent; [pen.color drawSwatchInRect:NSMakeRect(0, 0, 100, 100)];
Note that the modern Objective-C runtime can synthesize instance variables for properties; hence, explicit declaration of instance variables is not necessary, but still possible.
class Pen { private $_color; function __set($property, $value) { if ($property == 'Color') { return $this->_color = $value; } } function __get($property) { if ($property == 'Color') { return $this->_color; } } }
$p = new Pen(); $p->Color = ~$p->Color; // bitwise complement echo $p->Color;
Properties only work correctly for new-style classes (classes that have object
as a superclass), and are only available in Python 2.2 and newer (see the relevant section of the tutorial Unifying types and classes in Python 2.2). Python 2.6 added a new syntax involving decorators for defining properties; this only works on properties that are readable.
class Pen(object): def __init__(self): self._color = 0 # "private" variable @property def color(self): return self._color @color.setter def color(self, color): self._color = color
pen = Pen() # accessing: pen.color = ~pen.color # bitwise complement ...
class Pen def initialize @color = 0 end # there is actually a shortcut for these: attr_accessor :color will # synthetise both methods automatically, it was expanded for # compatibility with properties actually worth writing out def color @color end def color=(value) @color = value end end pen = Pen.new pen.color = ~pen.color
Public Class Pen Private m_Color As Integer ' Private field Public Property Color As Integer ' Public property Get Return m_Color End Get Set(ByVal Value As Integer) m_Color = Value End Set End Property End Class
' accessing: Dim pen As New Pen() ' ... pen.Color = Not pen.Color
' in a class named clsPen Private m_Color As Long Public Property Get Color() As Long Color = m_Color End Property Public Property Let Color(ByVal RHS As Long) m_Color = RHS End Property
' accessing: Dim pen As New clsPen ' ... pen.Color = Not pen.Color